from django.db import connection
from django.http import HttpResponseRedirect
from django.http import HttpResponse
from django.shortcuts import redirect, render
from django.views.generic import ListView, DetailView
from django.views.generic.edit import CreateView, FormView
from .models import Bookmarks
from .form import SearchForm


class HomePageView(ListView):
    model = Bookmarks
    bookmarks = Bookmarks.objects.all()
    template_name = "homepage.html"
    context_object_name = "object_list"


class NewBkmkPageView(CreateView):
    model = Bookmarks
    template_name = "new.html"
    fields = "__all__"


class DetailBkmkPageView(DetailView):
    model = Bookmarks
    template_name = "detail.html"


class SearchFormView(FormView):
    model = Bookmarks
    template_name = "search.html"
    form_class = SearchForm
    context_object_name = "object_list"

    def get_request(self, term, request):
        if request.method == "GET":
            form = SearchForm(request.GET) 
            if form.is_valid():
                form.save()
                return redirect(request, "/search/results/", form)
        else:
            form = SearchForm()       

        return HttpResponse(form)


class ResultsPageView(ListView):
    model = Bookmarks
    template_name = "results.html"
